// pages/personalMessage/personalMessage.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgSrc:'',
    info:[],
    zyjy:'',
    digest:'',
  },
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0]
        if (tempFilePaths) {
          that.setData({
            hiddenImg: false
          })
        }
        that.setData({
          imgSrc: tempFilePaths
        });
        that.upload();
      }
    })
  },
  upload: function (e) {
    var that = this;
    var imgSrc = that.data.imgSrc;
    console.log(imgSrc)
    wx.uploadFile({
      url: app.d.ceshiUrl + '/Api/Doctor/uploadimg',
      filePath: imgSrc,
      name: 'data',
      success: function (res) {
        console.log('uploadImage success, res is:', res);
        wx.request({
          url: app.d.ceshiUrl + '/Api/Doctor/saveImg',
          data: {
            uid: app.d.userId,
            photo_x: res.data,
          },
          header: {// 设置请求的 header
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            if (res.data.status == 1) {

            } else {
              wx.showToast({
                title: res.data.err,
              })
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络异常！',
              duration: 30000
            });
          },
        });
      },

      fail: function ({ errMsg }) {
        console.log('uploadImage fail, errMsg is', errMsg)
        return false;
      }
    })
  },
  update:function () {
    var that = this;
    var digest = that.data.digest;
    var zyjy = that.data.zyjy;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Doctor/update',
      method: "post",
      data: {
        uid: app.d.userId,
        digest:digest,
        zyjy:zyjy
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.err,
            duration:2000
          })
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Doctor/detail',
      method: "post",
      data: {
        uid: app.d.userId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          that.setData({
            info: res.data.info,
            zyjy: res.data.info.zyjy,
            digest: res.data.info.digest,
            imgSrc: res.data.info.photo_x,
          });
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },
  getdigest:function (e) {
    var digest = e.detail.value;
    this.setData({
      digest:digest
    });
  },
  getzyjy: function (e) {
    var zyjy = e.detail.value;
    this.setData({
      zyjy: zyjy
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})